package smtp;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.mail.PasswordAuthentication;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 * The login screen used to return the given username and pass
 * 
 * @author wendlandts, chandlerr, liw
 *
 */
public class LoginScreen extends JFrame{

	private String user;
	private String pass;
	private Driver driver;

	private JTextField userName = new JTextField("Username");
	private JPasswordField password = new JPasswordField();
	private JButton login = new JButton("Login");

	/**
	 * Takes in the driver so it can call on it to continue the program when the user enters their credentials
	 * @param driver
	 */
	public LoginScreen(Driver driver) {
		this.driver = driver;
		setUpUI();
	}

	/**
	 * Assembles all the components of the frame
	 * Adds correct listeners to respective components
	 */
	public void setUpUI() {
		// Set up main frame
		this.setSize(300, 150);
		this.setTitle("Email Client Login");
		this.setLayout(new GridLayout(2,1));
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setResizable(false);
		this.setLocation(500, 350);

		// Add listeners to the fields and button
		UserFocusListener userFocusListener = new UserFocusListener();
		EnterListener enterListener = new EnterListener();
		ButtonListener buttonListener = new ButtonListener();
		userName.addFocusListener(userFocusListener);
		userName.addKeyListener(enterListener);
		password.addKeyListener(enterListener);
		login.addActionListener(buttonListener);

		// Setup the input space
		JPanel inputPanel = new JPanel();
		inputPanel.setLayout(new GridLayout(2,1));
		inputPanel.add(userName);
		inputPanel.add(password);

		//Put the items in to the main frame
		this.add(inputPanel);
		this.add(login);

		// Make visible and request focus to get cursor off the username field
		this.setVisible(true);
		this.requestFocus();
	}
	
	/**
	 * Assemble the given information and send it back up to the driver
	 */
	public void assembleAndTransfer() {
		// Get the information from the text fields
		user = userName.getText();
		// Make sure that there is a @msoe.edu at the end of the username when
		// submitting it to the server
		if(!user.contains("@")) {
			user += "@msoe.edu";
			userName.setText(user);
			LoginScreen.this.paint(LoginScreen.this.getGraphics());
		}
		pass = new String(password.getPassword());
		
		driver.login(new PasswordAuthentication(user,pass));
	}

	/**
	 * Used to clear the text boxes when clicked
	 * @author wendlandts
	 *
	 */
	private class UserFocusListener implements FocusListener{
		boolean userClicked = false;
		@Override
		public void focusGained(FocusEvent e) {
			// Delete the current text in the field
			if(!userClicked) {
				userName.setText("");
				userClicked = true;
			}	
		}

		@Override
		public void focusLost(FocusEvent arg0) {
			// Do nothing
		}
	}

	/**
	 * Used to recognize when the enter button is hit in one of the text fields
	 * @author wendlandts
	 *
	 */
	private class EnterListener implements KeyListener {
		// When the enter button is pressed in one of the text areas, 
		// press the login button
		@Override
		public void keyPressed(KeyEvent e) {
			if(e.getKeyCode() == KeyEvent.VK_ENTER) {
				login.doClick();
			}
		}

		@Override
		public void keyReleased(KeyEvent e) {
			// Do nothing
		}

		@Override
		public void keyTyped(KeyEvent e) {
			// Do nothing
		}
	}

	/**
	 * To handle when the login button is pressed
	 * @author wendlandts
	 *
	 */
	private class ButtonListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent ae) {
			assembleAndTransfer();
		}
	}
}
